#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/shm.h>

typedef struct shared_memory{
	int flag;
	char text[2048];
};

int main()
{
	int shmid;
	void * shmaddr = NULL;
	int flag = 1;
	struct shared_memory *addr;
	char buf[2048] = {0};
	int running   = 1;

	shmid = shmget((key_t)2344, sizeof(struct shared_memory), 0660 | IPC_CREAT | IPC_EXCL);
	if(shmid == -1)
	{
		shmid = shmget((key_t)2344, sizeof(struct shared_memory), 0660 | IPC_EXCL);
		if(shmid == -1)
		{
			return -1;
		}
	}

	shmaddr = shmat(shmid, 0, 0);
	if(shmaddr = (void*) -1)
	{
		shmctl(shmid, IPC_RMID, NULL);
		return -1;
	}

	addr = (struct shared_memory *)shmaddr;

	while(1)
	{
		if(addr->flag)
		{
			sleep(1);
			printf("wating.......");
		}
		printf("please Enter somethings");

		fgets(buf, 2048 -1, stdin);
		strncpy(addr->text , buf, strlen(buf));
		addr->flag = 1;

		if(strcmp(buf, "end") == 0)
			running = 0;
	}

	if(shmdt(shmid) == -1)
	{
		return -1;
	}
	sleep(2);
	return 0;
}
